<?php
namespace Home\Controller;
use Think\Controller;
class UsergroupController extends CommonController {
    public function index(){
        $model = M('Usergroup'); 
        $rows = $model->select(); 
        $this->assign('rows', $rows);
        $this->display();

    }
    public function add(){
        if (IS_POST) {
            //过滤数据
            $data = I('post.');
            //设置自动验证规则 模型类文件中
            //设置自动完成规则 模型类文件中
            $model = D('Usergroup');

            //自动创建数据
            $resArr = $model->create($data); 

            if($resArr === false || empty($resArr)){
                $errMsg = $model->getError();
                $this->error($errMsg ? $errMsg : '未知错误');
            }
            
            if($model->add()){
                $this->success('添加成功!');
            }else{
                $this->error($model->getError());
            }
        }else{
            $this->display('edit');
        }
    }
    public function edit(){
        if (IS_POST) {
            //过滤数据
            $data = I('post.');
            $model = D('Usergroup'); 
            //设置自动验证规则 模型类文件中
            //设置自动完成规则 模型类文件中

            //自动创建数据
            $resArr = $model->create($data);
            if($resArr === false || empty($resArr)){
                $errMsg = $model->getError();
                $this->error($errMsg ? $errMsg : '未知错误');
            }

            if($model->save()){
                $this->success('更新成功');
            }else{
                $this->error($model->getError());
            }
        }else{
            $id = I('get.id');
            $model = M('Usergroup');
            $row = $model->find($id);

            $this->assign('row', $row);
            $this->display('edit');
        }
    }
    public function delDo(){
        $id = I('get.id');
        $model = M('Usergroup');

        if ($model->delete($id)) {
            $this->success('删除成功');
        }else{
            $this->error($model->getError());
        }
    }
    public function selectRule(){
        
        if (IS_POST) {

            $auth_config = C('AUTH_CONFIG');
            $auth_group_access = $auth_config['AUTH_GROUP_ACCESS'];
            $auth_group = $auth_config['AUTH_GROUP'];
            $auth_rule = $auth_config['AUTH_RULE'];
            $gid = I('post.gid');
            $new = I('post.newRules');
            $rules = implode(',',$new); 
            $data = array('id'=>$gid, 'rules'=>$rules);
            $model = M();
            if($model->table($auth_group)->save($data)){
                $this->success('处理成功',U('index')); 
            }else{
                $this->error($model->getError());
            }
            
        }else{
            //用户组id
            $gid = I('get.id');

            $auth_config = C('AUTH_CONFIG');
            $auth_group_access = $auth_config['AUTH_GROUP_ACCESS'];
            $auth_group = $auth_config['AUTH_GROUP'];
            $auth_rule = $auth_config['AUTH_RULE'];
            //规则列表
            $all_rules = M()
            ->table($auth_rule)
            ->where(array('status'=>1))
            ->select();
            //已选规则列表 
            $sel_str_rules = M()
            ->table($auth_group)
            ->field('rules')
            ->find($gid);
            $sel_rules = explode(",",$sel_str_rules['rules']);
            
            //用户组id  规则列表  已选规则
            $this->assign('gid',$gid);
            $this->assign('rows',$all_rules);
            $this->assign('selRules',$sel_rules);
            //$this->title = '规则列表列表';
            //分配视图
            $this->display();
        }
    }
/*
    public function selectRuleDo(){
        $auth_config = C('AUTH_CONFIG');
        $auth_group_access = $auth_config['AUTH_GROUP_ACCESS'];
        $auth_group = $auth_config['AUTH_GROUP'];
        $auth_rule = $auth_config['AUTH_RULE'];
        $gid = I('post.gid');
        $new = I('post.newRules');
        $rules = implode(',',$new); 
        $data = array('id'=>$gid, 'rules'=>$rules);
        $model = M();
        if($model->table($auth_group)->save($data)){
            $this->success('处理成功',U('index')); 
        }else{
            $this->error($model->getError());
        }
        
    }
*/
}
